package com.xyht.sca_s.student_manage_system.modules.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.goods.entity.SmsGoods;
import com.xyht.sca_s.student_manage_system.modules.goods.entity.SmsGoodsStock;
import com.xyht.sca_s.student_manage_system.modules.goods.entity.req.SmsGoodsAddReq;
import com.xyht.sca_s.student_manage_system.modules.goods.entity.req.SmsGoodsDeletedReq;
import com.xyht.sca_s.student_manage_system.modules.goods.entity.req.SmsGoodsUpdateReq;
import com.xyht.sca_s.student_manage_system.modules.goods.entity.resp.SmsGoodsResp;
import com.xyht.sca_s.student_manage_system.modules.goods.mapper.SmsGoodsClassificationMapper;
import com.xyht.sca_s.student_manage_system.modules.goods.mapper.SmsGoodsMapper;
import com.xyht.sca_s.student_manage_system.modules.goods.mapper.SmsGoodsStockMapper;
import com.xyht.sca_s.student_manage_system.modules.goods.service.SmsGoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 * 物品表(--迎新) 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2024-01-17
 */
@Service
public class SmsGoodsServiceImpl extends ServiceImpl<SmsGoodsMapper, SmsGoods> implements SmsGoodsService {

    @Resource
    private SmsGoodsMapper goodsMapper;
    @Resource
    private SmsGoodsClassificationMapper goodsClassificationMapper;
    @Resource
    private SmsGoodsStockMapper goodsStockMapper;

    @Override
    public ResponseResult addGoods(String userId, SmsGoodsAddReq addReq) {
        if (isNullOrEmpty(addReq.getGoodsName(),addReq.getIsOn())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        //校验物品名不能重复
        List<SmsGoods> goodsList = goodsMapper.selectList(new LambdaQueryWrapper<SmsGoods>()
                .eq(SmsGoods::getGoodsName, addReq.getGoodsName()));
        if (!goodsList.isEmpty()) {
            return CommonResult.failed(CommonCodeEnum.GOODS_NAME_EXIST);
        }
        SmsGoods smsGoods = new SmsGoods();
        smsGoods.setGoodsName(addReq.getGoodsName());
        smsGoods.setIsOn(addReq.getIsOn());
        smsGoods.setUserId(userId);
        goodsMapper.insert(smsGoods);
        return CommonResult.success();
    }

    @Override
    public ResponseResult deletedGoods(SmsGoodsDeletedReq deletedReq) {
        if (isNullOrEmpty(deletedReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsGoods smsGoods = goodsMapper.selectById(deletedReq.getId());
        if (isNullOrEmpty(smsGoods)) {
            return CommonResult.failed(CommonCodeEnum.GOODS_NOT_EXIST);
        }
        goodsMapper.deleteById(deletedReq.getId());
        return CommonResult.success();
    }

    @Override
    public ResponseResult updateGoods(SmsGoodsUpdateReq updateReq) {
        if (isNullOrEmpty(updateReq.getId(),updateReq.getGoodsName(),updateReq.getIsOn())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsGoods smsGoods = goodsMapper.selectById(updateReq.getId());
        if (isNullOrEmpty(smsGoods)) {
            return CommonResult.failed(CommonCodeEnum.GOODS_NOT_EXIST);
        }
        //校验物品名不能重复
        List<SmsGoods> goodsList = goodsMapper.selectList(new LambdaQueryWrapper<SmsGoods>()
                .eq(SmsGoods::getGoodsName, updateReq.getGoodsName())
                .ne(SmsGoods::getId, updateReq.getId()));
        if (!goodsList.isEmpty()) {
            return CommonResult.failed(CommonCodeEnum.GOODS_NAME_EXIST);
        }
        BeanUtils.copyProperties(updateReq,smsGoods);
        goodsMapper.updateById(smsGoods);
        return CommonResult.success();
    }

    @Override
    public ResponseResult getGoodsList(Integer pageNum, Integer pageSize, String name, Integer isOn) {
        Page<SmsGoods> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsGoods> goodsLambdaQueryWrapper = new LambdaQueryWrapper<SmsGoods>().orderByDesc(SmsGoods::getCreateTime);
        if (!isNullOrEmpty(name)) {
            goodsLambdaQueryWrapper.like(SmsGoods::getGoodsName,name);
        }
        if (!isNullOrEmpty(isOn)) {
            goodsLambdaQueryWrapper.like(SmsGoods::getIsOn,isOn);
        }
        goodsMapper.selectPage(page,goodsLambdaQueryWrapper);
        List<SmsGoodsResp> goodsList = page.getRecords()
                .stream()
                .map(smsGoods -> {
                    SmsGoodsResp smsGoodsResp = new SmsGoodsResp();
                    BeanUtils.copyProperties(smsGoods,smsGoodsResp);
                    List<SmsGoodsStock> smsGoodsStocks = goodsStockMapper.selectList(new LambdaQueryWrapper<SmsGoodsStock>()
                            .eq(SmsGoodsStock::getGoodsId, smsGoods.getId()));
                    int stockQuantity = 0;
                    if (!smsGoodsStocks.isEmpty()) {
                        for (SmsGoodsStock smsGoodsStock : smsGoodsStocks) {
                            stockQuantity += smsGoodsStock.getStockQuantity();
                        }
                    }
                    smsGoodsResp.setSumQuantity(stockQuantity);
                    return smsGoodsResp;
                })
                .collect(Collectors.toList());
        int total = (int) page.getTotal();
        return CommonResult.success(goodsList,total);
    }
}
